package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.listener.StoreClient;
import com.yunhai.device.mapper.CrrCleaningSummaryReportMapper;
import com.yunhai.device.service.CrrCleaningSummaryReportService;
import com.yunhai.device.service.DeviceCategoryService;
import com.yunhai.device.service.DeviceInfoService;
import com.yunhai.model.entity.crr.CrrCleaningSummaryReportEntity;
import com.yunhai.model.entity.dms.DmsDeviceCategoryEntity;
import com.yunhai.model.entity.dms.DmsDeviceInfoEntity;
import com.yunhai.model.vo.crr.CrrCleaningSummaryReportQuery;
import com.yunhai.model.vo.crr.CrrCleaningSummaryReportVo;
import com.yunhai.model.vo.tms.TmsStoreVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CrrCleaningSummaryReportServiceImpl implements CrrCleaningSummaryReportService {

    @Resource
    private CrrCleaningSummaryReportMapper crrCleaningSummaryReportMapper;
    @Resource
    private StoreClient storeClient;
    @Resource
    private DeviceCategoryService deviceCategoryService;
    @Resource
    private DeviceInfoService deviceInfoService;


    /**
     * 获取 CrrCleaningSummaryReport 分页列表
     */
    @Transactional
    public IPage getCrrCleaningSummaryReportPagination(PaginationVo paginationVo, CrrCleaningSummaryReportQuery crrCleaningSummaryReportQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<CrrCleaningSummaryReportEntity> lqs = new LambdaQueryWrapper();
        crrCleaningSummaryReportMapper.selectPage(page, lqs);

        List<CrrCleaningSummaryReportEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        //处理门店数据
        Set<Long> storeIds = record.stream().map(item->item.getStoreId()).collect(Collectors.toSet());
        //处理设备数据
        Set<Long> deviceIds = record.stream().map(item->item.getDeviceId()).collect(Collectors.toSet());
        //处理产品id
        Set<Long> productIds = record.stream().map(item->item.getMachineType()).collect(Collectors.toSet());
//
//



        //处理门店名称
        if( ObjectUtil.isNotEmptyList(storeIds) ){
            List<TmsStoreVo> storeList = storeClient.getTmsStoreByIds(storeIds.stream().toList());
            if( ObjectUtil.isNotEmptyList(storeList) ){
                for( CrrCleaningSummaryReportEntity item : record ){
                    storeList.stream().filter(row->row.getId().equals(item.getStoreId())).findFirst().ifPresent(row->{
                        item.setStoreName(row.getName());
                        item.setStoreNumber(row.getNumber());
                    });
                }
            }
        }

        //数据设备唯一id、设备名称
        if( ObjectUtil.isNotEmptyList(deviceIds) ){
            List<DmsDeviceInfoEntity> deviceList = deviceInfoService.getDeviceInfoListByIds(deviceIds);
            if( ObjectUtil.isNotEmptyList(deviceList) ){
                for( CrrCleaningSummaryReportEntity item : record ){
                    deviceList.stream().filter(row->row.getId().equals(item.getDeviceId())).findFirst().ifPresent(row->{
                        item.setMachineUniqueCode(row.getSn());
                    });
                }
            }
        }

        //处理种类
        if( ObjectUtil.isNotEmptyList(storeIds) ){
            List<DmsDeviceCategoryEntity> productList = deviceCategoryService.getProductListByIds(productIds.stream().toList());
            if( ObjectUtil.isNotEmptyList(productList) ){
                for( CrrCleaningSummaryReportEntity item : record ){
                    productList.stream().filter(row->row.getId().equals(item.getMachineType())).findFirst().ifPresent(row->{
                        item.setMachineTypeName(row.getName());
                    });
                }
            }
        }

        return page;
    }


    /**
     * 获取 CrrCleaningSummaryReport 所有数据
     *
     */
    @Transactional
    public List<CrrCleaningSummaryReportEntity> getCrrCleaningSummaryReportAllList( CrrCleaningSummaryReportQuery crrCleaningSummaryReportQuery ) throws Exception {

        LambdaQueryWrapper<CrrCleaningSummaryReportEntity> lqs = new LambdaQueryWrapper();
        List<CrrCleaningSummaryReportEntity> list = crrCleaningSummaryReportMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 CrrCleaningSummaryReport 详情
     */
    @Transactional
    public CrrCleaningSummaryReportEntity getCrrCleaningSummaryReportDetail( Long id) throws Exception {

        CrrCleaningSummaryReportEntity crrCleaningSummaryReportEntity = crrCleaningSummaryReportMapper.selectById(id);
        return crrCleaningSummaryReportEntity;
    }


    /**
     * 新增 CrrCleaningSummaryReport
     * */
    @Transactional
    public void addCrrCleaningSummaryReport(CrrCleaningSummaryReportVo crrCleaningSummaryReportVo) throws Exception {

        CrrCleaningSummaryReportEntity crrCleaningSummaryReportEntity = new CrrCleaningSummaryReportEntity();
        ReflectionCopy.copyProperties( crrCleaningSummaryReportEntity, crrCleaningSummaryReportVo );
        crrCleaningSummaryReportEntity.setCleaningDate(new Date());
        if( crrCleaningSummaryReportVo.getCleaningEndTime() != null
            && crrCleaningSummaryReportEntity.getCleaningStartTime() != null){
            long diffMilliseconds = crrCleaningSummaryReportVo.getCleaningEndTime().getTime()
                    - crrCleaningSummaryReportEntity.getCleaningStartTime().getTime();
            long diffMinutes = diffMilliseconds / (60 * 1000);
            crrCleaningSummaryReportEntity.setTotalCleaningDuration((int)diffMinutes);
        }

        crrCleaningSummaryReportMapper.insert(crrCleaningSummaryReportEntity);
        return;
    }


    /**
     * 更新 CrrCleaningSummaryReport
     * */
    @Transactional
    public void updateCrrCleaningSummaryReport( CrrCleaningSummaryReportVo crrCleaningSummaryReportVo) throws Exception {

        if( crrCleaningSummaryReportVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        CrrCleaningSummaryReportEntity crrCleaningSummaryReportEntity = new CrrCleaningSummaryReportEntity();
        ReflectionCopy.copyProperties( crrCleaningSummaryReportEntity, crrCleaningSummaryReportVo );
        crrCleaningSummaryReportMapper.updateById(crrCleaningSummaryReportEntity);
        return;
    }

    /**
     * 删除 CrrCleaningSummaryReport
     * */
    @Transactional
    public void batchRemoveCrrCleaningSummaryReport( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        crrCleaningSummaryReportMapper.deleteBatchIds(ids);
    }

}